﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WAContratos.Manager;
using WebContratos.Bean;



namespace WebContratos
{
    public partial class consultaProrroga : System.Web.UI.Page
    {
        public ContratoManager unContra = new ContratoManager();
        public ProrrogaBean unGaran = new ProrrogaBean();
        public ArrayList unArray = null;
        public string codContrato = "";
        public string codActa = "";
        public string tipo = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!(IsPostBack))
                {
                    codContrato = Convert.ToString(Session["codContrato"]);
                    tipo = Convert.ToString(Request.QueryString["tipo"]);

                    if (tipo.Equals("actualiza"))
                    {
                        codActa = Convert.ToString(Request.QueryString["codProrroga"]);

                        unContra = new ContratoManager();
                        unGaran = new ProrrogaBean();

                        unArray = unContra.ObtieneProrrogaContra(codContrato, codActa);

                        unGaran = (ProrrogaBean)unArray[2];

                        this.lblTipo.Text = tipo;

                        this.txtCodigoProrroga.Text = unGaran.CodigoProrroga;
                        this.txtDescripcion.Text = unGaran.Descripcion;
                        this.txtFechaInicio.Text = unGaran.FechaInicio;
                        this.txtFechaProrroga.Text = unGaran.FechaProrroga;
                        this.txtInforme.Text = unGaran.Informe;

                        this.txtCodContrato.Text = codContrato;

                    }
                    else
                    {
                        this.txtCodContrato.Text = codContrato;
                        this.txtCodigoProrroga.Enabled = true;
                        this.txtCodigoProrroga.ReadOnly = false;
                    }


                }
            }
            catch (Exception e2)
            {
                MsgBox1.alert(e2.Message);
            }
        }

        protected void btnGrabar_Click(object sender, EventArgs e)
        {
            string sql = "";



            if (this.lblTipo.Text.Equals("actualiza"))
            {
                sql = "update t_prorroga " +
                              " set descripcion = '" + this.txtDescripcion.Text + "'," +
                              "     fecha_inicio =  to_date('" + this.txtFechaInicio.Text + "','rrrr-mm-dd'), " +
                              "     fecha_prorroga =  to_date('" + this.txtFechaProrroga.Text + "','rrrr-mm-dd'), " +
                              "     informe = '" + this.txtInforme.Text + "'" +                              
                              " where codigo_contrato = '" + this.txtCodContrato.Text + "' and codigo_prorroga = '" + this.txtCodigoProrroga.Text + "'";

                cls_datos unDato = new cls_datos();

                bool result = unDato.insertaq(sql);

                /*if (result)
                    MsgBox1.alert("Se Actualizo Correctamente la Garantia No " + this.txtCodGarantia.Text);*/

                string jScript;
                jScript = "<script>consuPro('" + this.txtCodContrato.Text + "');</script>";
                //consuGaran('" + this.txtCodContrato.Text + "');</script>";



                //this.codContratoGar.Text = this.txtCodContrato.Text;


                Page.RegisterStartupScript("keyClientBlock", jScript);
            }
            else
            {


                sql = "insert into t_prorroga " +
                                     " (   codigo_contrato, " +
                                     "     CODIGO_PRORROGA, " +
                                     "     DESCRIPCION, " +
                                      "    FECHA_INICIO,  " +
                                      "    FECHA_PRORROGA, " +
                                      "    INFORME " +
                                      ") " +
                                      " values( " +
                                               "'" + this.txtCodContrato.Text + "'," +
                                               "'" + this.txtCodigoProrroga.Text + "'," +
                                               "'" + this.txtDescripcion.Text + "'," +
                                               "to_date('" + this.txtFechaInicio.Text + "','rrrr-mm-dd'), " +
                                               "to_date('" + this.txtFechaProrroga.Text + "','rrrr-mm-dd'), " +
                                               "'" + this.txtInforme.Text + "')";

                cls_datos unDato = new cls_datos();

                bool result = unDato.insertaq(sql);

                /*if (result)
                    MsgBox1.alert("Se Actualizo Correctamente la Garantia No " + this.txtCodGarantia.Text);*/

                string jScript;
                jScript = "<script>consuPro('" + this.txtCodContrato.Text + "');</script>";
                //consuGaran('" + this.txtCodContrato.Text + "');</script>";



                //this.codContratoGar.Text = this.txtCodContrato.Text;


                Page.RegisterStartupScript("keyClientBlock", jScript);
            }
        }
    }
}
